package com.guinea.core.spring;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import net.sf.navigator.menu.MenuRepository;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderServlet;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.guinea.account.service.DictionaryManager;
import com.guinea.account.service.MenuManager;
import com.guinea.account.service.RoleManager;
import com.guinea.svw.service.CommonManager;

public class MyContextLoaderServlet extends ContextLoaderServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(MyContextLoaderServlet.class);
	
    public void init() throws ServletException {
        super.init();
        setupContext(getServletContext());
    }
    
    public static void setupContext(ServletContext context) {
		ApplicationContext ctx = WebApplicationContextUtils
				.getRequiredWebApplicationContext(context);
		SessionFactory sessionFactory = (SessionFactory)ctx.getBean("sessionFactory");
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		
		MenuManager menuManager = (MenuManager) ctx.getBean("menuManager");
		MenuRepository repository = menuManager.getRepository();
		MenuRepository defaultRepository = (MenuRepository) context
				.getAttribute(MenuRepository.MENU_REPOSITORY_KEY);
		repository.setDisplayers(defaultRepository.getDisplayers());
		// get list of possible roles
		context.setAttribute("repository", repository);
		List menus = menuManager.getValid();
		context.setAttribute("menus",menus);

		RoleManager roleManager = (RoleManager) ctx.getBean("roleManager");
		List allRoles = roleManager.getValid();
		context.setAttribute("allRoles", allRoles);
		
		DictionaryManager dictionaryManager = (DictionaryManager) ctx.getBean("dictionaryManager");
		List rootDictionary = dictionaryManager.getRootNodes();
		context.setAttribute("rootDictionary", rootDictionary);
		List leafDictionary = dictionaryManager.getLeafNodes();
		context.setAttribute("leafDictionary", leafDictionary);
		
		tx.commit();
		
		CommonManager commonManager = (CommonManager) ctx.getBean("commonManager");
		List provinces = commonManager.getProvinces();
		context.setAttribute("provinces", provinces);
		List cities = commonManager.getCities();
		context.setAttribute("cities", cities);
		List districts = commonManager.getDistricts();
		context.setAttribute("districts", districts);
		String pcdJson = commonManager.convertPCDToJson(provinces,cities,districts);
		context.setAttribute("pcdJson",pcdJson);
		log.info("======================================Initialization complete [OK]");

	}
}
